package com.loong.common.alipay.utils;

import com.alipay.api.*;
import com.loong.common.alipay.config.AlipayConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description:
 * @Author: 刘群
 * @Date: 2020/11/19 10:49
 */
@Slf4j
public class AlipayClientUtil {

    /**
     * 获取AlipayClient对象
     * @return AlipayClient对象
     * @throws AlipayApiException
     */
    public static AlipayClient getAlipayClient() throws AlipayApiException {
        //创建证书有关的请求
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        //设置网关地址
        certAlipayRequest.setServerUrl(AlipayConfig.URL);
        //设置应用Id
        certAlipayRequest.setAppId(AlipayConfig.APP_ID);
        //设置应用私钥
        certAlipayRequest.setPrivateKey(AlipayConfig.APP_PRIVATE_KEY);
        //设置请求格式，固定值json
        certAlipayRequest.setFormat(AlipayConfig.FORMAT);
        //设置字符集
        certAlipayRequest.setCharset(AlipayConfig.CHARSET);
        //设置签名类型
        certAlipayRequest.setSignType(AlipayConfig.SIGNTYPE);
        //设置应用公钥证书路径
        certAlipayRequest.setCertPath(AlipayConfig.APP_CERT_PUBLIC_PATH);
        //设置支付宝公钥证书路径
        certAlipayRequest.setAlipayPublicCertPath(AlipayConfig.ALIPAY_CERT_PUBLIC_PATH);
        //设置支付宝根证书路径
        certAlipayRequest.setRootCertPath(AlipayConfig.ALIPAY_ROOT_CERT_PATH);
        //创建client
        return new DefaultAlipayClient(certAlipayRequest);
    }
}
